<?php
class eventHelper
{
	/*
	 * retrieves the top upcoming events
	 * takes a limit for the number of events to return
	 * returns all the events in an array
	 * $events = array(
	 * 		[ID] => array(
	 * 			'USER_ID'	=> userID		// primary key/id for event
	 * 			'NAME'		=> name
	 * 			'DESCRIPTION'		=> description
	 * 			'LOCATION'	=> location
	 * 			'HOSTED_BY'	=> hostName		// could be empty, but who is sponsoring the event
	 * 			'LINK'		=> url			// if the event has a webpage
	 * 			'IMAGE_PATH' => url			// image for the thumnail
	 * 			'START_TIME' => time
	 * 			'END_TIME'	=> time
	 * 		)
	 * )
	 * 
	 */
	static function getUpcomingEvents($limit = 6, $test = false)
	{
		if($test)
			$DB = database::getTestDB();
		else
			$DB = database::getDB();
		$date = ourDate::getDateString();
		
		/*
		$sql = "select *  from events where START_TIME >= '$date' and ACTIVE = 1 order by START_TIME limit $limit";
		//echo $sql; die();
		
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$events[$row['ID']] = array('USER_ID' => $row['USER_ID'], 'NAME' => $row['NAME'], 'DESCRIPTION' => $row['DESCRIPTION'], 'LOCATION' => $row['LOCATION'], 'HOSTED_BY' => $row['HOSTED_BY'], 'LINK' => $row['LINK'], 'IMAGE_PATH' => $row['IMAGE_PATH'], 'START_TIME' => $row['START_TIME'], 'END_TIME' => $row['END_TIME']);
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		return $events;
		*/
		
		$pq = $DB->prepare("SELECT * FROM events WHERE start_time >= ? AND active = 1 ORDER BY start_time LIMIT ?");
		$pq->execute(array($date, $limit));
		$result = $pq->fetchAll();
		
		//echo '<pre>'.print_r($result, true).'</pre>';
		
		return $result;
	}
	
	
	/*
	 * retrieves the upcoming events for the user
	 * takes the userID of the user and limit of upcoming events
	 * returns an array of the events
	 * $events = array(
	 * 		[ID] => array(
	 * 			'USER_ID'	=> userID		// primary key/id for event
	 * 			'NAME'		=> name
	 * 			'DESCRIPTION'		=> description
	 * 			'LOCATION'	=> location
	 * 			'HOSTED_BY'	=> hostName		// could be empty, but who is sponsoring the event
	 * 			'LINK'		=> url			// if the event has a webpage
	 * 			'IMAGE_PATH' => url			// image for the thumnail
	 * 			'START_TIME' => time
	 * 			'END_TIME'	=> time
	 * 		)
	 * )
	 * 
	 */
	static function getUsersUpcomingEvents($userID, $limit = 6, $test = false, $tags = false)
	{
		if($test)
			$DB = database::getTestDB();
		else
			$DB = database::getDB();
		$date = ourDate::getDateString();
		
		$events = array();
		$sql = "select e.* from events as e left join user_has_event as u on (u.EVENT_ID = e.ID) where e.START_TIME >= '$date' and u.USER_ID = $userID and ACTIVE = 1 order by e.START_TIME limit $limit";
		//echo $sql; die();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
		{
			$events[$row['ID']] = array('USER_ID' => $row['USER_ID'], 'NAME' => $row['NAME'], 'DESCRIPTION' => $row['DESCRIPTION'], 'LOCATION' => $row['LOCATION'], 'HOSTED_BY' => $row['HOSTED_BY'], 'LINK' => $row['LINK'], 'IMAGE_PATH' => $row['IMAGE_PATH'], 'START_TIME' => $row['START_TIME'], 'END_TIME' => $row['END_TIME']);
			
			if($tags)
			{
				$events[$row['ID']] = array('USER_ID' => $row['USER_ID'], 'NAME' => $row['NAME'], 'DESCRIPTION' => $row['DESCRIPTION'], 'LOCATION' => $row['LOCATION'], 'HOSTED_BY' => $row['HOSTED_BY'], 'LINK' => $row['LINK'], 'IMAGE_PATH' => $row['IMAGE_PATH'], 'START_TIME' => $row['START_TIME'], 'END_TIME' => $row['END_TIME']);
			
				$sqlTag = "select et.TAG_ID, t.DESCRIPTION from event_has_tag as et left join tags as t on (et.TAG_ID = t.ID) where et.EVENT_ID = ".$row['ID'];
				//if($row['ID'] == 18)
					//echo $sqlTag; die();
				$resultTag = mysqli_query($DB, $sqlTag);
				while($rowTag = mysqli_fetch_array($resultTag, MYSQLI_ASSOC))
					$events[$row['ID']]['TAGS'][$rowTag['TAG_ID']] = $rowTag['DESCRIPTION'];
			}
		}
		//echo '<pre>'.print_r($events, true).'</pre>'; die();
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		return $events;
	}
	
	
	/*
	 * retrieves all future events for the user
	 * takes the userID of the user
	 * returns an array of the events
	 * $events = array(
	 * 		[ID] => array(
	 * 			'USER_ID'	=> userID		// primary key/id for event
	 * 			'NAME'		=> name
	 * 			'DESCRIPTION'		=> description
	 * 			'LOCATION'	=> location
	 * 			'HOSTED_BY'	=> hostName		// could be empty, but who is sponsoring the event
	 * 			'LINK'		=> url			// if the event has a webpage
	 * 			'IMAGE_PATH' => url			// image for the thumnail
	 * 			'START_TIME' => time
	 * 			'END_TIME'	=> time
	 * 		)
	 * )
	 * 
	 */
	static function getUsersAllEvents($userID, $test = false)
	{
		if($test)
			$DB = database::getTestDB();
		else
			$DB = database::getDB();
		$date = ourDate::getDateString();
		
		$events = array();
		$sql = "select e.* from events as e left join user_has_event as u on (u.EVENT_ID = e.ID) where e.START_TIME >= '$date' and u.USER_ID = $userID and ACTIVE = 1  order by e.START_TIME";
		//echo $sql; die();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$events[$row['ID']] = array('USER_ID' => $row['USER_ID'], 'NAME' => $row['NAME'], 'DESCRIPTION' => $row['DESCRIPTION'], 'LOCATION' => $row['LOCATION'], 'HOSTED_BY' => $row['HOSTED_BY'], 'LINK' => $row['LINK'], 'IMAGE_PATH' => $row['IMAGE_PATH'], 'START_TIME' => $row['START_TIME'], 'END_TIME' => $row['END_TIME']);
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		return $events;
	}
	
	
	
	/*
	 * retrieves upcoming events with the certain tag id
	 * takes the id of the tag and a limit (defaults to 6)
	 * returns an array of the events
	 * $events = array(
	 * 		[ID] => array(
	 * 			'USER_ID'	=> userID		// primary key/id for event
	 * 			'NAME'		=> name
	 * 			'DESCRIPTION'		=> description
	 * 			'LOCATION'	=> location
	 * 			'HOSTED_BY'	=> hostName		// could be empty, but who is sponsoring the event
	 * 			'LINK'		=> url			// if the event has a webpage
	 * 			'IMAGE_PATH' => url			// image for the thumnail
	 * 			'START_TIME' => time
	 * 			'END_TIME'	=> time
	 * 		)
	 * )
	 * 
	 */
	static function getUpcomingEventsWithTag($ID, $limit = 6, $test = false)
	{
		if($test)
			$DB = database::getTestDB();
		else
			$DB = database::getDB();
		$date = ourDate::getDateString();
		
		$events = array();
		$sql = "select e.* from events as e left join user_has_event as u on (u.EVENT_ID = e.ID) where e.START_TIME >= '$date' and u.USER_ID = $ID and ACTIVE = 1  order by e.START_TIME";
		//echo $sql; die();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
		{
			$events[$row['ID']] = array('USER_ID' => $row['USER_ID'], 'NAME' => $row['NAME'], 'DESCRIPTION' => $row['DESCRIPTION'], 'LOCATION' => $row['LOCATION'], 'HOSTED_BY' => $row['HOSTED_BY'], 'LINK' => $row['LINK'], 'IMAGE_PATH' => $row['IMAGE_PATH'], 'START_TIME' => $row['START_TIME'], 'END_TIME' => $row['END_TIME']);
			
			$sqlTag = "select et.TAG_ID, t.DESCRIPTION from event_has_tag as et left join tags as t on (et.TAG_ID = t.ID) where et.TAG_ID = ".$row['ID'];
			//($row['ID'] == 18)
				//echo $sqlTag; die();
			$resultTag = mysqli_query($DB, $sqlTag);
			while($rowTag = mysqli_fetch_array($resultTag, MYSQLI_ASSOC))
				$events[$row['ID']]['TAGS'][$rowTag['TAG_ID']] = $rowTag['DESCRIPTION'];
		}
			
		//echo '<pre>'.print_r($events, true).'</pre>'; die();
		
		mysqli_free_result($result);
		mysqli_close($DB);
		
		return $events;
	}
	
	
	
	/*
	 * retrieves all events with the certain tag id
	 * takes the id of the tag
	 * returns an array of the events
	 * $events = array(
	 * 		[ID] => array(
	 * 			'USER_ID'	=> userID		// primary key/id for event
	 * 			'NAME'		=> name
	 * 			'DESCRIPTION'		=> description
	 * 			'LOCATION'	=> location
	 * 			'HOSTED_BY'	=> hostName		// could be empty, but who is sponsoring the event
	 * 			'LINK'		=> url			// if the event has a webpage
	 * 			'IMAGE_PATH' => url			// image for the thumnail
	 * 			'START_TIME' => time
	 * 			'END_TIME'	=> time
	 * 		)
	 * )
	 * 
	 */
	// this is not correct - needs to be fixed
	/*static function getAllEventsWithTag($ID)
	{
		$DB = database::getDB();
		$date = ourDate::getDateString();
		
		$sql = "select e.* from events as e left join event_has_tag as t on (e.ID = t.EVENT_ID) where e.START_TIME >= '$date' and t.TAG_ID = $ID and ACTIVE = 1 order by e.START_TIME";
		//echo $sql; die();
		$result = mysqli_query($DB, $sql);
		while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
			$events[$row['ID']] = array('USER_ID' => $row['USER_ID'], 'NAME' => $row['NAME'], 'DESCRIPTION' => $row['DESCRIPTION'], 'LOCATION' => $row['LOCATION'], 'HOSTED_BY' => $row['HOSTED_BY'], 'LINK' => $row['LINK'], 'IMAGE_PATH' => $row['IMAGE_PATH'], 'START_TIME' => $row['START_TIME'], 'END_TIME' => $row['END_TIME']);
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		return $events;
	}*/
	
	
	/*
	 * takes the user id (sanatized)
	 * returns array of 3 recom events
	 */
	static function getUserRecomEvents($userID)
	{
		$DB = database::getDB();
		$date = ourDate::getDateString();
		
		//get the next 10 events the user is reg for, with tags
		$allUpcoming = eventHelper::getUsersUpcomingEvents($userID, 10, false, true);
		//echo '<pre>'.print_r($allUpcoming, true).'</pre>'; die();
		
		if(!empty($allUpcoming))
		{
			foreach($allUpcoming as $ID => $event)
			{
				$eventIDs[] = $ID;
				foreach($event['TAGS'] as $ID => $name)
					$tagIDs[] = $ID;
			}
			
			
			$sql = "select e.ID from events as e left join event_has_tag as et on (et.EVENT_ID = e.ID) where e.ID not in (".implode(', ', $eventIDs).") and et.TAG_ID in (".implode(', ', $tagIDs).") group by e.ID limit 3";
			
			$result = mysqli_query($DB, $sql);
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
				$eventID[] = $row['ID'];
		}
		else
			$eventID = array();
			
		if(count($eventID) <= 2)
		{
			switch(count($eventID))
			{
				case 2: $sql = "select ID  from events where START_TIME >= '$date' and ACTIVE = 1 order by START_TIME limit 1"; break;
				case 1: $sql = "select ID  from events where START_TIME >= '$date' and ACTIVE = 1 order by START_TIME limit 2"; break;
				case 0: $sql = "select ID  from events where START_TIME >= '$date' and ACTIVE = 1 order by START_TIME limit 3"; break;
			}
			
			$result = mysqli_query($DB, $sql);
			while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
				$eventID[] = $row['ID'];
		}
		//echo '<pre>'.print_r($eventID, true).'</pre>'; die();
		
		foreach($eventID as $key => $ID)
			$eventDetails[$ID] = eventClass::getEventDetails($ID);
		//echo '<pre>'.print_r($eventDetails, true).'</pre>'; die();
			
		mysqli_free_result($result);
		mysqli_close($DB);
		
		return $eventDetails;
	}
	
}
?>